/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>May 3, 20132:19:04 PM</p>
 * @author Ding Zhe
 * @version 1.0
 */
package com.umpay.privi.ejb.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.privi.common.BeanManager;
import com.umpay.privi.common.Constants;
import com.umpay.privi.common.ReqMessage;
import com.umpay.privi.common.ResMessage;
import com.umpay.privi.common.RetCode;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.on.sys.PrivilegeDao;
import com.umpay.privi.dao.on.sys.RoleDao;
import com.umpay.privi.dao.on.sys.RolePriviDao;
import com.umpay.privi.dao.on.sys.UserDao;
import com.umpay.privi.dao.on.sys.UserRoleDao;
import com.umpay.privi.exception.BusinessException;
import com.umpay.privi.helper.ReqParaCheckHelper;
import com.umpay.privi.model.sys.Privilege;
import com.umpay.privi.model.sys.Role;
import com.umpay.privi.model.sys.RolePrivi;
import com.umpay.privi.model.sys.User;
import com.umpay.privi.model.sys.UserRole;
import com.umpay.privi.service.sys.RoleServiceRemote;
import com.umpay.privi.util.SeqUtil;
import com.umpay.privi.util.StringUtil;

/** 
 * desc:角色EJB
 * <p>创建人：Ding Zhe 创建日期：May 3, 2013 </p>
 * @version V1.0  
 */
@TransactionManagement(TransactionManagementType.BEAN)
@SuppressWarnings("unchecked")
public class RoleServiceBean implements RoleServiceRemote{
	private static Logger logger = LoggerFactory.getLogger(RoleServiceBean.class);
	
	
	public ResMessage addRoleAndPrivileges(ReqMessage req) {
		
		ResMessage res = new ResMessage();
		logger.info(String.format("[RoleServiceBean]addRoleAndPrivileges请求参数：%s", req.toString()));
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.addRoleAndPrivileges.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		Short sysId = Short.parseShort(req.getString(XmlData.SYSID));//系统编号
		String operUserId=req.getString(XmlData.OPERUSERID);
		int roleId = -1;
		User operUser=null;
		try {
			roleId = SeqUtil.genSequence(Constants.ROLE_SEQ)+100;//+100的是新添加，否则就是初始化sql生成 
			logger.info(String.format("[创建角色权限]角色编号为：%s", roleId));
			//查询操作员
			Map<String, Object> qryMap = new HashMap<String, Object>();
			qryMap.put(XmlData.USERID, operUserId);
			qryMap.put(XmlData.SYSID, sysId);
			UserDao userDao = BeanManager.getBean("userDao", UserDao.class);
			operUser = userDao.getUser(qryMap);
			if(operUser == null)throw new BusinessException(RetCode.NO_DATA,"操作员不存在！");
		}catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据库出错");
		}  catch (BusinessException e) {
			logger.error(String.format("[创建角色权限]生成角色编号异常,retCode=%s,retMsg=%s", e.getCode(),e.getMessage()));
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
			return res;
		}
		Role role = new Role();
		role.setRoleId(roleId);//系统生成角色号
		role.setRoleName(req.getString(XmlData.ROLE_NAME));//角色名称
		role.setRoleType(Short.parseShort(req.getString(XmlData.ROLE_TYPE)));//角色类型
		role.setSysId(sysId);
		role.setCreator(operUser.getMediaId()); //创建者的mediaId
		role.setModUser(operUser.getMediaId()); //修改者的mediaId
		role.setDescription(req.getString(XmlData.DESCRIPTION));//角色描述
		role.setState(Short.parseShort(req.getString(XmlData.STATE)));//状态
		logger.info(String.format("[创建角色权限]角色信息：%s", role));
		
		List<RolePrivi> rolePriviList = (List<RolePrivi>)req.get(XmlData.ROLE_PRIVILEGE_LIST);
		if (null == rolePriviList || rolePriviList.isEmpty() ) {
			logger.error("[创建角色权限]权限列表为空");
			res.setRetCode(RetCode.NO_DATA);
			res.setRetMsg("权限信息不存在");
			return res;
		}
		
		try {
			//插入角色信息
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			roleDao.insert(role);
			//插入用户角色信息
			UserRole userRole=new UserRole();
			userRole.setUserId(operUserId);//创建者的userId
			userRole.setRoleId(roleId);
			userRole.setFlag(Short.parseShort(Constants.FLAG_NORMAL));//正常
			userRole.setIsAssignable(Short.parseShort(Constants.IS_ASSIGNABLE_YES));//0：可分配
			userRole.setIsUsable(Short.parseShort(Constants.IS_USABLE_NO));//1：不可使用；
			UserRoleDao userRoleDao = BeanManager.getBean("userRoleDao", UserRoleDao.class);
			userRoleDao.insert(userRole);
			//批量插入角色权限信息
			logger.info(String.format("[创建角色权限]角色与权限关系信息，个数%s", rolePriviList.size()));
			RolePriviDao rolePriviDao = BeanManager.getBean("rolePriviDao", RolePriviDao.class);
			rolePriviDao.insertBatch(rolePriviList,roleId);
			
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[创建角色权限]数据库异常：", e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据库错误");
		}catch (Exception e) {
			logger.error("[创建角色权限]未知异常：", e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		
		return res;
	}
	//TODO:接口文档中没有
	public ResMessage closeRole(ReqMessage req) {
		ResMessage res = new ResMessage();
		logger.info(String.format("[RoleServiceBean]closeRole请求参数：%s", req.toString()));
		Short sysId = Short.parseShort(req.get(XmlData.SYSID).toString());
		Integer roleId = Integer.parseInt(req.getString(XmlData.ROLE_ID));
		
		try {
			//判断角色是否在使用
			if (isUsingRole(roleId, sysId)) {
				//当有用户使用该角色时，不能被关闭
				throw new BusinessException(RetCode.ROLE_IS_USING,"角色" + roleId + "正在被使用");
			}
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			int updateCount = roleDao.updateState(roleId, Short.parseShort(Constants.STATE_CANCLE), sysId);
			if (1 != updateCount) {
				logger.info(String.format("[关闭角色]更新记录数为%s", updateCount));
				throw new BusinessException(RetCode.ROLE_ISNOT_EXIT,"角色" + roleId + "不存在");
			}
			
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[关闭角色]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (BusinessException e) {
			logger.error(String.format("[关闭角色]异常，错误码：%s,错误信息%s。",e.getCode(),e.getMessage()));
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		} catch (Exception e) {
			logger.error("[根据角色编号查询角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		
		return res;
	}

	public ResMessage deleteRole(ReqMessage req) {
		ResMessage res = new ResMessage();
		logger.info(String.format("[RoleServiceBean]deleteRole请求参数：%s", req.toString()));
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.deleteRole.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		Short sysId = Short.parseShort(req.getString(XmlData.SYSID));
		Integer roleId = Integer.parseInt(req.getString(XmlData.ROLE_ID));
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put(XmlData.SYSID, sysId);
		paraMap.put(XmlData.ROLE_ID, roleId);
		
		try {
			//判断角色是否在使用
			if (isUsingRole(roleId, sysId)) {
				//当有用户使用该角色时，不能被删除
				throw new BusinessException(RetCode.ROLE_IS_USING,"角色" + roleId + "正在被使用");
			}
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			roleDao.delete(paraMap);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[删除角色]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (BusinessException e) {
			logger.error(String.format("[删除角色]异常，错误码：%s,错误信息%s。",e.getCode(),e.getMessage()));
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		} catch (Exception e) {
			logger.error("[删除角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}
	
	/**
	 * desc:角色是否正在使用(是否有操作员和业务管理员在使用)
	 * <p>创建人：Ding Zhe , May 6, 2013 4:20:25 PM</p>
	 * @param roleId 角色编号
	 * @param sysId 系统编号
	 * @return
	 * @throws DataAccessException 
	 */
	private boolean isUsingRole(int roleId, short sysId) throws DataAccessException {
		boolean result = false;
		
		UserDao userDao = BeanManager.getBean("userDao", UserDao.class);
		//查询使用该角色号的用户
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put(XmlData.ROLE_ID, roleId);
		paraMap.put(XmlData.SYSID, sysId);
		paraMap.put(XmlData.OPERRANK, Constants.RANK_SYS_ADMIN);// rank>1 查询是否有操作员和业务管理员在使用
		List<User> users = userDao.findByRoleId(paraMap);
		if (null != users && !users.isEmpty()) {
			result = true;
		}
		
		return result;
	}

	public ResMessage getRoleById(ReqMessage req) {
		ResMessage res = new ResMessage();
		logger.info(String.format("[RoleServiceBean]getRoleById请求参数：%s", req.toString()));
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.getRoleById.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			logger.info(String.format("[根据角色编号查询角色]请求参数：%s", req.toString()));
			Role role = roleDao.get(Integer.parseInt(req.getString(XmlData.ROLE_ID)));
			logger.info(String.format("[根据角色编号查询角色]查询结果：%s", role));
			res.put(XmlData.RESULT, role);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[根据角色编号查询角色]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[根据角色编号查询角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage modifyRole(ReqMessage req) {
		logger.info(String.format("[角色修改]请求参数：%s", req.toString()));
		ResMessage res = new ResMessage();
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.modifyRole.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			
			String state = req.getString(XmlData.STATE);
			Short sysId = Short.parseShort(req.getString(XmlData.SYSID));
			Integer roleId = Integer.parseInt(req.getString(XmlData.ROLE_ID));
			//状态由开启到关闭，判断角色是否在使用
			if (state.equals(Constants.STATE_CANCLE)&&isUsingRole(roleId, sysId)) {
				//当有用户使用该角色时，不能被删除
				throw new BusinessException(RetCode.ROLE_IS_USING,"角色" + roleId + "正在被使用");
			}
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			roleDao.update(req.getReqMap());
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[修改角色]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (BusinessException e) {
			logger.error(String.format("[修改角色]异常，错误码：%s,错误信息%s。",e.getCode(),e.getMessage()));
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		} catch (Exception e) {
			logger.error("[修改角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage modifyRoleAndPrivileges(ReqMessage req) {
		logger.info(String.format("[角色预览]请求参数：%s", req.toString()));
		ResMessage res = new ResMessage();
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.modifyRoleAndPrivileges.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			Integer roleId = Integer.parseInt(req.getString(XmlData.ROLE_ID));
			//如果记录不存在将抛业务异常
			List<RolePrivi> rolePriviList = (List<RolePrivi>)req.get(XmlData.ROLE_PRIVILEGE_LIST);
			if (null == rolePriviList || rolePriviList.isEmpty() ) {
				throw new BusinessException(RetCode.NO_DATA,"权限信息不存在");
			}
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			RolePriviDao rolePriviDao = BeanManager.getBean("rolePriviDao", RolePriviDao.class);
			Map<String,Object> paraMap=new HashMap<String,Object>();
			paraMap.put(XmlData.ROLE_ID, roleId);
			paraMap.put(XmlData.FLAG, 1);//0 正常 1 删除
			//1.更新角色信息
			roleDao.update(req.getReqMap());
			//2.标记删除该角色下所有关系
			rolePriviDao.updateByRoleId(paraMap);
			//3批量修改或新增该角色权限关系.
			rolePriviDao.batchInsertOrUpdate(rolePriviList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[修改角色]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据库错误");
		} catch (BusinessException e) {
			logger.error(String.format("[修改角色]异常，错误码：%s,错误信息%s。",e.getCode(),e.getMessage()));
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		} catch (Exception e) {
			logger.error("[修改角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage previewRolePrivilege(ReqMessage req) {
		logger.info(String.format("[角色预览]请求参数：%s", req.toString()));
		ResMessage res = new ResMessage();
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.previewRolePrivilege.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			PrivilegeDao privilegeDao = BeanManager.getBean("privilegeDao", PrivilegeDao.class);
			Role role = roleDao.get(Integer.parseInt(req.getString(XmlData.ROLE_ID)));
			logger.info(String.format("[角色预览]查询结果：%s", role));
			List<Privilege> privilegeList=privilegeDao.findListByRoleId(req.getReqMap());
			res.put(XmlData.ROLE, role);
			res.put(XmlData.PRIVILIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[根据角色编号查询角色]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[根据角色编号查询角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage queryRolesByName(ReqMessage req) {
		ResMessage res = new ResMessage();
		logger.info(String.format("[RoleServiceBean]queryRolesByName请求参数：%s", req.toString()));
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.queryRolesByName.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			logger.info(String.format("[根据角色名称查询角色]请求参数：%s", req));
			List<Role> roleList = roleDao.findBy(req.getReqMap());
			logger.info(String.format("[根据角色名称查询角色]查询结果：%s", roleList));
			res.put(XmlData.RESULT_LIST, roleList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[根据角色名称查询角色]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[根据角色名称查询角色]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage queryRoles(ReqMessage reqMessage) {
		ResMessage resMessage = new ResMessage();//创建报文返回对象
		logger.info(String.format("[根据条件查询所有角色][RoleServiceBean]queryRoles请求参数：%s", reqMessage.toString()));
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(reqMessage, "RoleServiceBean.queryRoles.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			UserDao userDao = BeanManager.getBean("userDao", UserDao.class);
			Short sysId = Short.parseShort(reqMessage.getString(XmlData.SYSID));
			String operUserId = StringUtil.trim(reqMessage.get(XmlData.OPERUSERID));
			Map<String, Object> reqMap=reqMessage.getReqMap();
			//查询操作员信息
			Map<String, Object> qryMap = new HashMap<String, Object>();
			qryMap.put(XmlData.USERID, operUserId);
			qryMap.put(XmlData.SYSID, sysId);
			User operUser = userDao.getUser(qryMap);
			if(operUser == null)throw new BusinessException(RetCode.OPER_NOT_EXIST,"操作员不存在！");
			//根据操作员身份查询不同的权限
			reqMap.put(XmlData.OPERRANK, operUser.getRank());
			List<Role> roleList=roleDao.findRoleList(reqMap);
			resMessage.put(XmlData.RESULT_LIST, roleList);
			resMessage.setRetCode(RetCode.SUCCESS);
			resMessage.setRetMsg("成功");
		}catch (BusinessException e) {
			e.printStackTrace();
			logger.error("请求数据不正确",e);
			resMessage.setRetCode(RetCode.REQPARAM_ERR);
			resMessage.setRetMsg(e.getMessage());
		} catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.DB_ERROR);
			resMessage.setRetMsg("数据库出错");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.UNKNOWN);
			resMessage.setRetMsg("未知错误");
		}
		return resMessage;
	}
	
	public ResMessage queryRolesWithPage(ReqMessage reqMessage) {
		logger.info(String.format("[根据条件分页查询所有角色][RoleServiceBean]queryRolesWithPage请求参数：%s", reqMessage.toString()));
		ResMessage resMessage = new ResMessage();//创建报文返回对象
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(reqMessage, "RoleServiceBean.queryRolesWithPage.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			Integer startIndex = Integer.parseInt(reqMessage.getString(XmlData.START_INDEX));//开始记录索引
			Integer pageSize = Integer.parseInt(reqMessage.getString(XmlData.PAGE_SIZE));//每页显示条数
			Map<String, Object> reqMap=reqMessage.getReqMap();
			List<Role> roleList=roleDao.findPageList(reqMap, startIndex, pageSize);
			Integer totalNum = roleDao.findPageList_count(reqMap);
			resMessage.put(XmlData.RESULT_LIST, roleList);
			resMessage.put(XmlData.TOTAL_NUM, totalNum);
			resMessage.setRetCode(RetCode.SUCCESS);
			resMessage.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.DB_ERROR);
			resMessage.setRetMsg("数据库出错");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.UNKNOWN);
			resMessage.setRetMsg("未知错误");
		}
		return resMessage;
	}

	public ResMessage queryCreatedRoles(ReqMessage req) {
		logger.info("[RoleServiceBean]queryCreatedRoles请求数据："+req);
		ResMessage res = new ResMessage();
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "RoleServiceBean.queryCreatedRoles.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		try {
			RoleDao roleDao = BeanManager.getBean("roleDao", RoleDao.class);
			logger.info(String.format("[RoleServiceBean]queryCreatedRoles请求参数：%s", req));
			List<Map<String, Object>> roleList = roleDao.findRolesByCreator(req.getReqMap());
			logger.info(String.format("[RoleServiceBean]queryCreatedRoles查询结果：%s", roleList));
			res.put(XmlData.RESULT_LIST, roleList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		} catch (DataAccessException e) {
			logger.error("[RoleServiceBean]queryCreatedRoles异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[RoleServiceBean]queryCreatedRoles未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		logger.info("[RoleServiceBean]queryCreatedRoles响应数据："+res);
		return res;
	}
	
	public ResMessage queryAuthRolesByUserId(ReqMessage reqMessage) {
		logger.info("[UserServiceBean]queryAuthRolesByUserId请求数据："+reqMessage);
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(reqMessage, "RoleServiceBean.queryAuthRolesByUserId.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		UserRoleDao userRoleDao = BeanManager.getBean("userRoleDao",UserRoleDao.class);
		
		ResMessage resMessage = new ResMessage();//创建报文返回对象
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			String sysId = StringUtil.trim(reqMessage.getString(XmlData.SYSID));
			String operUserId = StringUtil.trim(reqMessage.getString(XmlData.OPERUSERID));
			//获取操作用户信息
			Map<String, Object> qryMap = new HashMap<String, Object>();
			qryMap.put(XmlData.USERID, operUserId);
			qryMap.put(XmlData.SYSID, sysId);
			qryMap.put(XmlData.ISASSIGNABLE, "0");
			qryMap.put(XmlData.ROLETYPE, "2");
			logger.info("查询用户可授权的角色：" + qryMap);
			resultList = userRoleDao.queryUserRolesByUserId(qryMap);
			logger.info("查询用户可授权的角色返回结果：" + resultList);
			
			resMessage.put(XmlData.RESULT_LIST, resultList);
			resMessage.setRetCode(RetCode.SUCCESS);
			resMessage.setRetMsg("查询用户可授权的角色成功");
		} catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.DB_ERROR);
			resMessage.setRetMsg("数据库出错");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.UNKNOWN);
			resMessage.setRetMsg("未知错误");
		}
		logger.info("[UserServiceBean]queryAuthRolesByUserId响应数据："+resMessage);
		return resMessage;
	}
	public ResMessage queryRolesIdByUserId(ReqMessage reqMessage) {
		logger.info("[UserServiceBean]queryRoleSIdByUserId请求数据："+reqMessage);
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(reqMessage, "RoleServiceBean.queryAuthRolesByUserId.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		UserRoleDao userRoleDao = BeanManager.getBean("userRoleDao",UserRoleDao.class);
		
		ResMessage resMessage = new ResMessage();//创建报文返回对象
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			String sysId = StringUtil.trim(reqMessage.getString(XmlData.SYSID));
			String operUserId = StringUtil.trim(reqMessage.getString(XmlData.OPERUSERID));
			//获取操作用户信息
			Map<String, Object> qryMap = new HashMap<String, Object>();
			qryMap.put(XmlData.USERID, operUserId);
			qryMap.put(XmlData.SYSID, sysId);
			logger.info("查询用户的角色：" + qryMap);
			resultList = userRoleDao.queryUserRolesByUserId(qryMap);
			logger.info("查询用户的角色返回结果：" + resultList);
			
			resMessage.put(XmlData.RESULT_LIST, resultList);
			resMessage.setRetCode(RetCode.SUCCESS);
			resMessage.setRetMsg("查询用户的角色成功");
		} catch (DataAccessException e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.DB_ERROR);
			resMessage.setRetMsg("数据库出错");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resMessage.setRetCode(RetCode.UNKNOWN);
			resMessage.setRetMsg("未知错误");
		}
		logger.info("[UserServiceBean]queryRoleSIdByUserId响应数据："+resMessage);
		return resMessage;
	}
}
